package com.naiterui.ehp.bs.doctor.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.core.ParameterizedTypeReference;

import com.naiterui.ehp.bp.bo.b2c.ProductInventoryBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;
import com.naiterui.ehp.bs.doctor.vo.medication.MedicatioInventorySaleVO;

/**
 * 药品相关调用b2c接口类
 *
 * @author lichaopi
 */
public class B2cGoodsAPIUtil {
    /**
     * b2c药品库存
     */
    private static final String API_QUERY_INVENTORY_INFO = ConfigUtil.getString("api.query.inventory.info");

    /**
     * b2c药品库存
     *
     * @param params
     *
     * @return
     *
     * @throws BusinessException
     */
    public static Map<Long, MedicatioInventorySaleVO> getInventoryInfoList(List<Map<String, Object>> params)
            throws BusinessException {
        Map<String, String> map = new HashMap<>();
        Map<Long, MedicatioInventorySaleVO> returnMap = new HashMap<>();
        // sku_id,num结构，返回上一层看结构
        String goods = JsonMapper.toJson(params);
        map.put("goods", goods);
        List<ProductInventoryBO> data = RestHttpUtil.post()
                                                    .url(API_QUERY_INVENTORY_INFO)
                                                    .paramMap(map)
                                                    .executeData(new ParameterizedTypeReference<ResponseVO<List<ProductInventoryBO>>>() {
                                                    });
        data.forEach(productInventoryBO -> {
            MedicatioInventorySaleVO infoVo = new MedicatioInventorySaleVO();
            Long skuId = productInventoryBO.getSkuId();
            infoVo.setSkuId(skuId);
            infoVo.setIsShort(productInventoryBO.getShorted());
            infoVo.setStockNum(productInventoryBO.getStockNum());
            infoVo.setIsPresell(productInventoryBO.getPresell());
            infoVo.setPresellInfo(productInventoryBO.getPresellInfo());
            infoVo.setIslimit(productInventoryBO.getLimitSale());
            infoVo.setSkuLimitInfo(productInventoryBO.getSkuLimitInfo());
            infoVo.setPatientLimitInfo(productInventoryBO.getPatientLimitInfo());
            infoVo.setSale(productInventoryBO.getSale());
            infoVo.setDosageWeek(productInventoryBO.getDosageWeek());
            infoVo.setDosageMonth(productInventoryBO.getDosageMonth());
            infoVo.setSixtyDosage(productInventoryBO.getSixtyDosage());
            infoVo.setNumber(productInventoryBO.getNumber());
            infoVo.setInventoryWarningValue(productInventoryBO.getInventoryWarningValue());
            infoVo.setInventoryWarning(productInventoryBO.getInventoryWarning());
            returnMap.put(skuId, infoVo);
        });
        return returnMap;
    }

}
